
import React from 'react'
import { Button, Table,Popconfirm, message } from 'antd';
import { inject, observer } from 'mobx-react'






@inject("xinJianYouHuiJuan")
@observer
class ViewCoupons extends React.Component {


  selChuShiHua = ()=>{
    let temp = this.props.location.search.split('&')

    let data = {
      couponType: parseInt(temp[1].split('=')[1]),
      couponId: parseInt(temp[0].split('=')[1])
    }
    this.props.xinJianYouHuiJuan.selDanZhangYouHuiJuan(data)
  }

  componentWillMount() {
    this.selChuShiHua()
  }

  state = {
    selectedRowKeys: [], // Check here to configure the default column
  };

  onSelectChange = selectedRowKeys => {
    console.log(selectedRowKeys);
    this.setState({ selectedRowKeys });
  };

  haha = () => {
    this.props.history.push('/Home/cuXiao/CouPon/YongHu')
  }

  render() {

    let { danZhangYouHuiJuanList } = this.props.xinJianYouHuiJuan//单张优惠卷List

    let delDanZhang = () => {//批量删除
      if (this.state.selectedRowKeys === []) {
        this.props.xinJianYouHuiJuan.delDanZhangYouHuiJuan(this.state.selectedRowKeys).then(data=>{
        data == 200 ? this.selChuShiHua() : message.warning('出错了')
      })
      }else{
        message.warning('请先选择要删除的数据')
      }
      
    }

    let delDanZhangYouHuiJuan = (row) => {//删除优惠券
      this.props.xinJianYouHuiJuan.delDanZhangYouHuiJuan([row]).then(data=>{
        data == 200 ? this.selChuShiHua() : message.success('出错了')
      })
    }

    const columns = [
      {
        key:'dcoupon_id',
        align:'center',
        title: '编号',
        dataIndex: 'dcoupon_id',
      },
      {
        key:'dcoupon_id',
        align:'center',
        title: '优惠卷序列号',
        dataIndex: 'dcoupon_num',
      },
      {
        key:'dcoupon_id',
        align:'center',
        title: '类型',
        dataIndex: 'dcoupon_id',
        render: () => {
          if(this.props.location.search.split('&')[1].split("=")[1] === '1' ) return <span>用户发放</span>
          else if(this.props.location.search.split('&')[1].split("=")[1] === '2' ) return <span>商品发放</span>
          else if(this.props.location.search.split('&')[1].split("=")[1] === '3' ) return <span>线下发放</span>
          else if(this.props.location.search.split('&')[1].split("=")[1] === '4' ) return <span>关注发放</span>
          else if(this.props.location.search.split('&')[1].split("=")[1] === '5' ) return <span>订单发放</span>
          else if(this.props.location.search.split('&')[1].split("=")[1] === '6' ) return <span>团长优惠</span>
          else if(this.props.location.search.split('&')[1].split("=")[1] === '7' ) return <span>积分兑换</span>
        }
      },
      {
        key:'dcoupon_id',
        align:'center',
        title: '订单号',
        dataIndex: 'order_code',
      },
      {
        key:'dcoupon_id',
        align:'center',
        title: '使用会员',
        dataIndex: 'member_nn',
      },
      {
        key:'dcoupon_id',
        align:'center',
        title: '使用时间',
        dataIndex: 'dcoupon_ut',
      },
      {
        key:'dcoupon_id',
        align:'center',
        dataIndex: 'dcouponId',
        title: 'Address',
        render: (dcouponId,row,key) => (
          <>
          <Popconfirm onConfirm={delDanZhangYouHuiJuan.bind(this, row.dcoupon_id)} title="确认删除" okText="是" cancelText="取消">
          <Button type="primary" danger>删除</Button>
           </Popconfirm>
          </>
        ),
      },
    ];

    const { selectedRowKeys } = this.state;

    const rowSelection = {
      // columnTitle:'全选',
      selectedRowKeys,
      onChange: this.onSelectChange,
      selections: [
        Table.SELECTION_ALL,
        Table.SELECTION_INVERT,
        {
          key: 'dcouponId',
          text: 'Select Odd Row',
          onSelect: changableRowKeys => {
            let newSelectedRowKeys = [];
            newSelectedRowKeys = changableRowKeys.filter((key, index) => {
              if (index % 2 !== 0) {
                return false;
              }
              return true;
            });
            this.setState({ selectedRowKeys: newSelectedRowKeys });
          },
        },
        {
          key: 'key',
          text: '',
          onSelect: changableRowKeys => {
            console.log(changableRowKeys)
            let newSelectedRowKeys = [];
            newSelectedRowKeys = changableRowKeys.filter((key, index) => {
              if (index % 2 !== 0) {
                return true;
              }
              return false;
            });
            this.setState({ selectedRowKeys: newSelectedRowKeys });
          },
        },
      ],
    };

    let pagination = {
      defaultPageSize: 5,
    }

    return (
      <div>
        <h1>查看优惠卷</h1>
        <Table rowSelection={rowSelection} pagination={pagination} columns={columns} dataSource={danZhangYouHuiJuanList} />
        <Popconfirm onConfirm={delDanZhang} title="确认删除" okText="是" cancelText="取消">
          <Button type="primary" danger>批量删除</Button>

           </Popconfirm>
      </div>
    )
  }
}

export default ViewCoupons
